<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link type="text/css" rel="stylesheet" href="/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="/css/ico.css" />
<link type="text/css" rel="stylesheet" href="/css/tooltip.css" />
<link type="text/css" rel="stylesheet" href="/css/util.css" />
<link type="text/css" rel="stylesheet" href="/css/upload.css" />
<link type="text/css" rel="stylesheet" href="/css/switch.css" />
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/clipboard.min.js"></script>
<script type="text/javascript" src="/js/tooltip.js"></script>
<script type="text/javascript" src="/js/util.js"></script>
<script type="text/javascript" src="/js/plupload.full.min.js"></script>
<script type="text/javascript" src="/js/upload.js"></script>
<title>Z盘</title>
<style type="text/css">
body {
	margin: 0.5em;
}

#title {
	font-size: 2em;
}

#operation-left {
	float: left;
	margin-left: 5em;
}

#operation-right {
	float: right;
	margin-right: 5em;
}

#table td:nth-child(1) {
	width: 1em;
}

#table td:nth-child(3) {
	opacity: 0;
	width: 13em;
}

#table tr:hover td:nth-child(3) {
	opacity: 1;
}

#table td:nth-child(4) {
	width: 6em;
}

#table td:nth-child(5) {
	width: 4em;
}

#table td:nth-child(6) {
	width: 11em;
}
</style>
<script>
	/* 自动加载 */
	$(function() {
		initResult();
		/* 全选checkbox添加监听事件 */
		$("#selectAll").click(function() {
			if ($(this).is(":checked")) {
				$("input[name='idcb']").prop("checked", true);
			} else {
				$("input[name='idcb']").prop("checked", false);
			}
		});
		$("input:radio[name='order'],input:checkbox[name='orderDesc']").click(function() {
			var orderBy = "";
			var orderText = $("input[name='order']:checked").val();
			var orderDescText = $("input[name='orderDesc']:checked").val();
			if (orderText != "null") {
				orderBy = orderText;
				if (orderDescText) {
					orderBy += " desc";
				}
			}
			initResult(orderBy);
		});
	});
	function initResult(orderBy) {
		$.ajax({
			url : (typeof (orderBy) == "undefined") ? "" : ("?orderBy=" + orderBy),
			type : "post",
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			success : function(data) {
				if (data.code == 2002) { //文件不存在
					showTips(null, data.msg);
					return false;
				}
				var d = data.data;
				var folderIdAndName = ""; //附加文件夹id和名字
				var f = d.folderInfo;
				if (f == null) {
					$("#title").text("我的文件");
					$("#current-location").empty();
					$("#current-location").append("当前位置：<a href='/user/user/file/info/'>根目录</a>");
					if (typeof (orderBy) == "undefined") {
						upload("/user/file/upload/", d.sizeone + "mb")
					}
					$("#order").show();
				} else {
					$("#title").text(f.name);
					$("#current-location").empty();
					$("#current-location").append("当前位置：<a href='/user/user/file/info/'>根目录</a> / <a href='/user/folder/file/info/" + f.timestamp + "'>" + f.name + "</a>");
					folderIdAndName = "?folderTimestamp=" + f.timestamp + "&folderName=" + f.name;
					$("#newFolderBtn").remove();
					upload("/user/file/upload/" + f.id, d.sizeone + "mb");
					$("#order").hide();
				}
				if (data.code == 0) {
					var list = d.files;
					if (list.length == 0) {
						showTips(null, "文件夹为空！", "success");
						return false;
					}
					$("#fileList").empty();
					var tr,
						id,
						name,
						suffix,
						description,
						timestamp,
						size,
						status,
						uuid,
						date,
						isfolder,
						isshare,
						islimit,
						issearch,
						pwd,
						previewFile,
						shareFile,
						clickFile,
						clickDownload;
					for (var i in list) {
						id = list[i].id;
						name = list[i].name;
						isfolder = list[i].isfolder;
						suffix = (isfolder == 1) ? "folder" : getFileSuffix(name);
						description = list[i].description;
						isshare = list[i].isshare,
						islimit = list[i].islimit,
						issearch = list[i].issearch,
						pwd = list[i].pwd;
						timestamp = list[i].timestamp;
						size = (isfolder == 1) ? "" : bytes2Size(list[i].length);
						status = (isshare == 1) ? (pwd == "" ? "共享" : "密码") : "";
						uuid = list[i].uuid;
						date = time2Date(list[i].time);
						clickFile = (isfolder == 0) ? ("/user/file/info/" + timestamp + folderIdAndName)
							: ("/user/folder/file/info/" + timestamp + folderIdAndName);
						editFile = "<button onClick='editFile1()' class='btn btn-xs btn-primary'>编辑</button>";
						shareFile = "<button onClick='shareFile()' class='btn btn-xs btn-info'>分享</button>";
						previewFile = (isfolder == 1) ? "" : "<button onClick='previewFile()' class='btn btn-xs btn-warning'>预览</button>";
						downloadFile = (isfolder == 1) ? "<button onClick='downloadFolder()' class='btn btn-xs btn-success'>下载</button>"
							: ("<a href='/file/download/" + uuid + "' target='_blank'><button class='btn btn-xs btn-success'>下载</button></a>");
						tr = "<tr id='" + id + "' name='" + name + "' description='" + description + "' isfolder='" + isfolder + "' isshare='" + isshare + "' islimit='" + islimit + "' issearch='" + issearch + "' pwd='" + pwd + "' timestamp='" + timestamp + "' uuid='" + uuid + "'>";
						tr += "<td><input type='checkbox' name='idcb' value='" + uuid + "'></td>";
						tr += "<td><a href='" + clickFile + "'><img class='ico ico-" + suffix + "'>" + name + "</a></td>";
						tr += "<td>" + editFile + "&ensp;" + shareFile + "&ensp;" + previewFile + "&ensp;" + downloadFile + "</td>";
						tr += "<td>" + size + "</td>";
						tr += "<td>" + status + "</td>";
						tr += "<td>" + date + "</td></tr>";
						$("#fileList").append(tr);
					}
				} else { //其他错误
					showTips(null, data.msg);
					return false;
				}
			},
			error : function(e) {
				showTips(null, "网络异常，请重试！", "warning");
			}
		});
	}
	function uploadFile() {
		var e = window.event;
		var btn = $(e.target || e.srcElement);
		if ($("#upload").is(":hidden")) {
			$("#upload").show();
			btn.html("点击隐藏");
		} else {
			$("#upload").hide();
			btn.html("上传文件");
		}
		var l = btn.offset().left; //按钮左坐标
		var bt = btn.offset().top; //按钮上坐标
		var bh = btn.outerHeight(); //按钮高
		var t = bt + bh + 5;
		$("#upload").css({
			"position" : "absolute",
			"z-index" : "10000",
			"top" : t,
			"left" : l
		});
	}
	function newFolder() {
		var e = window.event;
		var t = $(e.target || e.srcElement);
		var h = "<table><tr><td>新建文件夹&ensp;</td><td><input type='text' id='newFolderName' class='form-control' placeholder='请输入文件夹名'></td><td>&ensp;<button class='btn btn-success' onClick='newFolderOk()'>确定</button></td></tr></table>"
		tips(t, h);
	}
	function newFolderOk() {
		var name = $("#newFolderName").val();
		if (name.length == 0) {
			$("#newFolderName").css("background", "#faa");
			return false;
		}
		$.ajax({
			url : "/user/file/newFolder/" + name,
			type : "post",
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			success : function(data) {
				if (data.code == 0) {
					d = data.data;
					var tr = "<tr id='" + d.id + "' name='" + d.name + "' isfolder='1' isshare='0' islimit='0' issearch='0' pwd timestamp='" + d.timestamp + "' uuid='" + d.uuid + "'>";
					tr += "<td><input type='checkbox' name='idcb' value='" + d.uuid + "'></td>";
					tr += "<td><a href='/user/folder/file/info/" + d.timestamp + "'><img class='ico ico-folder'>" + d.name + "</a></td>";
					tr += "<td><button onclick='editFile1()' class='btn btn-xs btn-primary'>编辑</button> <button onclick='shareFile()' class='btn btn-xs btn-info'>分享</button>  <button onclick='downloadFolder()' class='btn btn-xs btn-success'>下载</button></td>";
					tr += "<td></td>";
					tr += "<td></td>";
					tr += "<td>" + time2Date(d.time) + "</td>";
					$("#fileList").prepend(tr);
					tips();
					showTips(null, "文件夹已创建！", "success");
				} else {
					showTips(null, data.msg);
				}
			},
			error : function(e) {
				showTips(null, "网络异常，请重试！", "warning");
			}
		});
	}
	function deleteFile() {
		var e = window.event;
		var t = $(e.target || e.srcElement);
		var h = "<table><tr><td>确定删除吗？&ensp;</td><td><button class='btn btn-success' onClick='deleteFileOk()'>确定</button></td></tr></table>"
		tips(t, h);
	}
	function deleteFileOk() {
		list = getCheckBoxValue();
		if (list.length == 0) {
			showTips(null, "没有文件被选择！", "warning");
			tips();
			return false;
		}
		$.ajax({
			url : "/user/file/delete",
			type : "post",
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			data : JSON.stringify(list),
			success : function(data) {
				if (data.code == 0) {
					var d = data.data;
					for (var i in d) {
						$("#" + d[i]).remove();
					}
					showTips(null, "文件已删除！", "success");
				} else {
					showTips(null, data.msg);
				}
			},
			error : function(e) {
				showTips(null, "网络异常，请重试！", "warning");
			}
		});
		tips();
	}
	function editFile1() {
		var e = window.event;
		var t = $(e.target || e.srcElement);
		var tr = t.parent().parent();
		var name = tr.attr("name");
		var description = tr.attr("description");
		var h = "<table class='st'><tr><td class='s1'>文件名</td>"
			+ "<td class='s2'><input type='text' class='form-control' id='name'></td></tr>"
			+ "<tr><td class='s1'>描述</td>"
			+ "<td class='s2'><textarea rows='5' class='form-control' id='description'></textarea></td></tr>"
			+ "<tr><td class='s1'></td><td class='s2'><button class='btn btn-success' id='editFile'>编辑</button></td></tr></table>"
			+ "<style>.st td{padding:0.5em 0 0 0.5em;}.s1{text-align:right;width:4em;}.s2{width:26em;}</style>";
		tips(t, h);
		$("#name").val(name);
		$("#description").val(description);
		$("#editFile").click(function() {
			editFileOk(t);
		});
	}
	function editFileOk(t) {
		var tr = t.parent().parent();
		var id = tr.attr("id");
		var isfolder = tr.attr("isfolder");
		var name = $("#name").val();
		var description = $("#description").val();
		$.ajax({
			url : "/user/file/edit",
			type : "post",
			data : JSON.stringify({
				id : id,
				name : name,
				description : description
			}),
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			success : function(data) {
				if (data.code == 0) {
					tips();
					tips(t, "修改成功！");
					tr.attr("name", name);
					tr.attr("description", description);
					var a = tr.children("td").eq(1).children("a").eq(0);
					a.empty();
					var suffix = (isfolder == 1) ? "folder" : getFileSuffix(name);
					var h = "<img class='ico ico-" + suffix + "'>" + name;
					a.append(h);
				} else {
					tips();
					tips(t, data.msg);
				}
			},
			error : function(e) {
				tips();
				tips(t, "网络异常，请重试！");
			}
		});
	}
	function shareFile() {
		var e = window.event;
		var t = $(e.target || e.srcElement);
		var tr = t.parent().parent();
		var timestamp = tr.attr("timestamp");
		var name = tr.attr("name");
		var pwd = tr.attr("pwd");
		var isfolder = tr.attr("isfolder");
		var isshare = tr.attr("isshare");
		var issearch = tr.attr("issearch");
		var islimit = tr.attr("islimit");
		var url = window.location.protocol + "//" + window.location.host + (isfolder == 1 ? "/f/" : "/z/") + timestamp;
		var link = (pwd.length != 0) ? (url + " 密码：" + pwd) : (url);
		var text = "分享文件" + (isfolder == 1 ? "夹" : "") + "【" + name + "】 链接地址： " + link;
		var h = "<table class='st'><tr><td class='s1'>分享</td><td class='s2'><input type='checkbox' class='switch' id='isshare' /></td>"
			+ "<td class='s1'><span class='hide1'>密码</span></td>"
			+ "<td class='s2'><input type='checkbox' class='switch hide1' id='pwd'/><label id='pwdText' class='hide1'></label></td><td class='s1'></td><td class='s3'></td></tr>"
			+ "<tr class='hide1'><td class='s1'>可搜索到</td>"
			+ "<td class='s2'><input type='checkbox' class='switch hide1' id='issearch' /></td>"
			+ "<td class='s1'>需要登录</td>"
			+ "<td class='s2'><input type='checkbox' class='switch hide1' id='islimit' /></td><td class='s1'></td><td class='s3'></td></tr>"
			+ "<tr class='hide1'><td class='s1'>分享</td>"
			+ "<td colspan='4'><label class='form-control' id='linkText'></label></td>"
			+ "<td class='s3'><button class='btn btn-info' id='copyLinkBtn'>复制</button></td></tr>"
			+ "<tr><td></td><td><button class='btn btn-success' id='shareEdit'>编辑</button></td></tr></table>"
			+ "<style>.st{background:#ddd;width:40em;}.st td{padding:0.5em 0 0 0.5em;}.s1{text-align:right;width:5em;}.s2{width:8em;}.s3{width:5em;}#pwdText{display:inline;}</style>";
		tips(t, h);
		$("#isshare").prop("checked", isshare == 1);
		$("#pwd").prop("checked", pwd != "");
		$("#pwdText").text(pwd);
		$("#issearch").prop("checked", issearch == 1);
		$("#islimit").prop("checked", islimit == 1);
		if (isshare == 0) {
			$(".hide1").hide()
		}
		if (pwd == "") {
			$("#pwdText").hide();
		}
		$("#linkText").text(link);
		$("#copyLinkBtn").click(function() {
			copyLink("#copyLinkBtn", "分享文件" + (isfolder == 1 ? "夹" : "") + "【" + name + "】下载地址: " + link);
		});
		$("#isshare").click(function() {
			if ($(this).is(":checked")) {
				$(".hide1").show();
				if (!$("#pwd").is(":checked")) {
					$("#pwdText").hide();
				}
			} else {
				$(".hide1").hide();
			}
		});
		$("#pwd").click(function() {
			var pwd;
			var url = window.location.protocol + "//" + window.location.host + (isfolder == 1 ? "/f/" : "/z/") + timestamp;
			if ($(this).is(":checked")) {
				$("#pwdText").show();
				pwd = randomFilePwd();
			} else {
				$("#pwdText").hide();
			}
			if (pwd != undefined) {
				link = url + " 密码：" + pwd;
			} else {
				link = url;
			}
			$("#pwdText").text(pwd);
			$("#linkText").text(link);
			$("#copyLinkBtn").unbind("click");
			$("#copyLinkBtn").click(function() {
				copyLink("#copyLinkBtn", "分享文件" + (isfolder == 1 ? "夹" : "") + "【" + name + "】下载地址: " + link);
			});
		});
		$("#shareEdit").click(function() {
			shareFileEdit(t);
		});
	}
	function shareFileEdit(t) {
		var tr = t.parent().parent();
		var id = tr.attr("id");
		var isshare = $("#isshare").is(":checked") ? 1 : 0;
		var issearch = $("#issearch").is(":checked") ? 1 : 0;
		var islimit = $("#islimit").is(":checked") ? 1 : 0;
		var pwd = $("#pwd").is(":checked") ? $("#pwdText").text() : "";
		$.ajax({
			url : "/user/file/edit",
			type : "post",
			data : JSON.stringify({
				id : id,
				isshare : isshare,
				issearch : issearch,
				islimit : islimit,
				pwd : pwd
			}),
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			success : function(data) {
				if (data.code == 0) {
					tips();
					tips(t, "修改成功！");
					tr.attr("pwd", pwd);
					tr.attr("isshare", isshare);
					tr.attr("issearch", issearch);
					tr.attr("islimit", islimit);
					var status = (isshare == 1) ? (pwd == "" ? "共享" : "密码") : "";
					tr.children("td").eq(4).text(status);
				} else {
					tips();
					tips(t, data.msg);
				}
			},
			error : function(e) {
				tips();
				tips(t, "网络异常，请重试！");
			}
		});
	}
</script>
</head>
<body>
	<div id="top">
		<div id="title"></div>
		<div id="current-location"></div>
		<div id="operation">
			<div id="operation-left">
				<button class="btn btn-success" onClick="uploadFile()" id="uploadBtn">上传文件</button>
				<button class="btn btn-default" onClick="newFolder()" id="newFolderBtn">新建文件夹</button>
				<span id="order"> &emsp;排序&emsp;<input type="radio" value="null" name="order" checked>默认&emsp;<input type="radio" value="name" name="order">文件名&emsp;<input type="radio"
					value="time" name="order">上传时间&emsp;<input type="radio" value="length" name="order">文件大小&emsp;<input type="radio" value="isshare" name="order">状态&emsp;<input
					type="checkbox" value="1" name="orderDesc">倒序
				</span>
			</div>
			<div id="operation-right">
				<button class="btn btn-primary" onClick="downloadPackage()">打包下载</button>
				<button class="btn btn-danger" onClick="deleteFile()">批量删除</button>
			</div>
		</div>
	</div>
	<div id="table">
		<table class="table table-hover table-striped table-condensed">
			<thead>
				<tr>
					<th>
						<input type="checkbox" id="selectAll">
					</th>
					<th>文件名</th>
					<th></th>
					<th>文件大小</th>
					<th>状态</th>
					<th>上传时间</th>
				</tr>
			</thead>
			<tbody id="fileList"></tbody>
		</table>
	</div>
	<div id="upload"></div>
</body>
</html>
